<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.8/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="editor">
    <div class="layui-form-item" id="id_div_wang"></div>

    <div class="layui-form-item">
        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
    </div>
</div>
<script src="/lib/layui-v2.6.8/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script>
    layui.use(['form', 'wangEditor'], function () {
        var form = layui.form,
            layer = layui.layer,
            wangEditor = layui.wangEditor,
            $ = layui.jquery;

        $.get('/admin/user/getCurrent', {}, function (re) {
            if (re.code == 0) {
                layui.use('watermark', function (watermark) {
                    watermark({watermark_txt: re.data.name});
                });
            }
        }, 'json');

        let id = layui.router().search.id;
        if (id == '' || isNaN(id)) {
            layer.alert('页面参数错误,请检查!');
            return false;
        }

        var editor = null;

        let contentIde = 1;

        $.get('/admin/item/info', {'id': id}, function (re) {
            if (re.code == 0) {
                editor = new wangEditor('#id_div_wang');
                editor.config.uploadImgServer = "/admin/static/create";
                editor.config.uploadFileName = 'file';
                editor.config.pasteFilterStyle = false;
                editor.config.uploadImgMaxLength = 1;
                editor.config.uploadImgHooks = {
                    timeout: function (xhr, editor) {
                        layer.msg('上传超时！')
                    },
                    customInsert: function (insertImg, result, editor) {
                        if (result.code == 0) {
                            insertImg(result.data.src);
                        } else {
                            layer.msg(result.msg);
                        }
                    }
                };
                editor.config.customAlert = function (info) {
                    layer.msg(info);
                };
                editor.create();
                editor.txt.html(re.data.content);
            } else {
                layer.msg('内容加载失败, 请检查 !');
            }
        }, 'json');

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            if (editor === null) {
                layer.msg('编辑器初始化失败！');
                return false;
            }
            $.post('/admin/item/update', {
                'id': id,
                'content_ide': contentIde,
                'content': editor.txt.html()
            }, function (re) {
                let msg = '';
                if (re.code == 0) {
                    msg = '保存成功';
                } else {
                    msg = re.msg;
                }
                let index = layer.alert(msg, {
                    title: '保存结果'
                }, function () {
                    layer.close(index);
                    if (re.code == 0) {
                        parent.layui.table.reload('currentTableId');
                        let iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }
                });
            }, 'json');
            return false;
        });

    });
</script>
</body>
</html>